<template>
	<z-paging-swiper>
		<template #top>
			<view class="zy-status-bar"></view>
			<zy-tab ref="tabs" :tabs="tabList" @change="tabsChange"></zy-tab>
		</template>

		<!-- @transition="swiperTransition" -->
		<swiper style="height: 100%;" :current="current" @animationfinish="swiperAnimationfinish">
			<swiper-item class="swiper-item" v-for="(item, index) in tabList" :key="index">
				<home-item :tabIndex="index" :currentIndex="current"></home-item>
			</swiper-item>
		</swiper>
		
		<template>
			<view class="zy-row tip-bg zy-p-sm" @click="toRanking">
				<image class="zy-img-sm-circle" :src="getTestImage()"></image>
				<view class="zy-text-whitegrey-sm zy-ml-sm">想购物+1000</view>
			</view>
		</template>

		<uni-popup ref="pay" type="bottom" :is-mask-click="false">
			<pay payType="3"></pay>
		</uni-popup>
	</z-paging-swiper>
</template>

<script>
	import constant from '@/common/constant.js';
	import TicketUtil from '@/main/common/TicketUtil.js';
	import CacheUtil from '@/main/common/CacheUtil';
	import RouteUtil from '@/main/common/RouteUtil';
	export default {
		data() {
			return {
				tabList: ['同城', '附近', '推荐'],
				current: 0,
			};
		},
		onLoad() {
			if(CacheUtil.getToken()){
				this.requestUser();
				this.requestLatlng();
			}
		},
		onShow() {
			setTimeout(() => {
				if(CacheUtil.getToken()){
					this.showDialogTicket(); //为啥要延迟，防止读写配置文件慢于
				}
			}, 500)
		},
		methods: {
			//tabs通知swiper切换
			tabsChange(index) {
				this.current = index;
			},
			//swiper滑动中
			// swiperTransition(e) {
			// 	this.$refs.tabs.setDx(e.detail.dx);
			// },
			//swiper滑动结束
			swiperAnimationfinish(e) {
				this.current = e.detail.current;
				// this.$refs.tabs.unlockDx();
				this.$refs.tabs.setIndex(e.detail.current);
			},

			//自己的
			showDialogTicket() {
				TicketUtil.isOpen().then(res => {
					if (res) {
						uni.hideTabBar()
						this.$refs.pay.open()
					} else {
						uni.showTabBar()
						this.$refs.pay.close()
					}
				})
			},
			requestUser() {
				if (CacheUtil.getLogin()) {
					uni.$http.getUser();
				}
			},
			requestLatlng() {
				if (!uni.$constant.online && CacheUtil.getUserId()) {
					uni.getLocation({
						success: res => {
							console.log("requestLatlng: " + JSON.stringify(res));
							let params = {
								lat: res.latitude,
								lon: res.longitude
							}
							uni.$http.updateUser(params)
						}
					});
				}
			},
			toRanking(){
				RouteUtil.toRanking()
			}
		}
	}
</script>

<style lang="scss" scoped>
	
	.tip-bg{
		position: absolute;
		left: 30rpx;
		bottom: 100rpx;
		z-index: 99;
		width: 280rpx;
		height: 80rpx;
		background-color: rgba(#000, 0.5);
		border-radius: 40rpx;
		
	}
</style>